local skill = fk.CreateSkill {
  name = "3s__chenyong",
}

Fk:loadTranslationTable{
  ["3s__chenyong"] = "沉勇",
  [":3s__chenyong"] = "游戏开始时，你从牌堆获得两张【无懈可击】且你可以于出牌阶段内重铸【无懈可击】。仅有/不止一名角色使用牌的回合结束时，你可获得弃牌堆内一张【无懈可击】/将一张【无懈可击】置于牌堆顶并令一名角色失去1点体力。",
  ["#3s__chenyong-recast"] = "沉勇：你可以重铸一张【无懈可击】",
  ["#3s__chenyong-obtain"] = "沉勇：你可以获得弃牌堆内一张【无懈可击】",
  ["#3s__chenyong-put"] = "沉勇：你可以将一张【无懈可击】置于牌堆顶并令一名角色失去1点体力",
  ["#3s__chenyong-choose"] = "沉勇：选择一名角色失去1点体力",
  
  ["$3s__chenyong1"] = "泰山崩于前而色不变，麋鹿兴于左而目不瞬。",
  ["$3s__chenyong2"] = "临危不乱，方显大将之风。",
}

-- 游戏开始时获得两张无懈可击
skill:addEffect(fk.GameStart, {
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(skill.name)
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local cards = player.room:getCardsFromPileByRule("nullification", 2, "drawPile")
    if #cards == 2 then
      player.room:obtainCard(player, cards, true, fk.ReasonJustMove, player, skill.name)
    end
  end,
})

-- 出牌阶段内可重铸无懈可击
skill:addEffect("active", {
  prompt = "#3s__chenyong-recast",
  target_num = 0,
  min_card_num = 1,
  card_filter = function(self, player, to_select, _)
    return Fk:getCardById(to_select).trueName == "nullification" and
    table.contains(player:getCardIds("he"), to_select)
  end,
  target_filter = Util.FalseFunc,
  on_use = function(self, room, effect)
    room:recastCard(effect.cards, effect.from)
  end
})

-- 回合结束时处理
skill:addEffect(fk.TurnEnd, {
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(skill.name) then
      local room = player.room
      -- 使用事件记录器获取本回合使用牌的角色
      local cardUseEvents = room.logic:getEventsByRule(GameEvent.UseCard, 999, function(e)
        return e.data.from
      end, nil, Player.HistoryTurn)
      local cardUsers = {}
      for _, event in ipairs(cardUseEvents) do
        local fromId = event.data.from.id
        table.insertIfNeed(cardUsers, fromId)
      end
      if #cardUsers == 1 then
        return table.find(room.discard_pile, function (id)
          return Fk:getCardById(id).trueName == "nullification"
        end)
      elseif #cardUsers > 1 then
        return table.find(player:getCardIds("he"), function (id)
          return Fk:getCardById(id).trueName == "nullification"
        end)
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    -- 使用事件记录器获取本回合使用牌的角色
    local cardUseEvents = room.logic:getEventsByRule(GameEvent.UseCard, 999, function(e)
      return e.data.from
    end, nil, Player.HistoryTurn)
    -- 统计使用牌的角色（去重）
    local cardUsers = {}
    for _, event in ipairs(cardUseEvents) do
      local fromId = event.data.from.id
      table.insertIfNeed(cardUsers, fromId)
    end
    local userCount = #cardUsers
    if userCount == 1 then
      local cards = room:getCardsFromPileByRule("nullification", 1, "discardPile")
      if #cards > 0 then
        room:obtainCard(player, cards, true, fk.ReasonJustMove, player, skill.name)
      end
    elseif userCount > 1 then
      -- 不止一名角色使用牌，可以将无懈可击置于牌堆顶并令角色失去体力
      local targets, cards = room:askToChooseCardsAndPlayers(player, {
        min_num = 1,
        max_num = 1,
        min_card_num = 1,
        max_card_num = 1,
        pattern = "nullification",
        targets = room.alive_players,
        skill_name = skill.name,
        prompt = "#3s__chenyong-put",
        cancelable = true
      })
      if #targets > 0 and #cards > 0 then
        -- 将无懈可击置于牌堆顶
        room:moveCardTo(cards[1], Card.DrawPile, nil, fk.ReasonJustMove, skill.name)
        -- 令目标失去1点体力
        room:loseHp(targets[1], 1, skill.name)
      end
    end 
  end,
})

return skill